﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreFrame.Business;
using System.Data;

namespace MarchEstate.Manager.Dao.Init
{
    public class SysInitDao : AbstractDao
    {
        public DataTable GetRoomList(string _areacode, string _buildingCode, string _roomname, CoreFrame.Core.Orm.PageInfo page)
        {
            string strsql = @" SELECT     a.RoomId, a.RoomNumber, d.AreaName, e.BuildingName,b.OwnerId, c.Name, (CASE WHEN a.UseType = 1 THEN '住宅' ELSE '住改商' END) AS UseType
                                FROM         WY_Room AS a LEFT OUTER JOIN
                                                      WY_RoomOwnerRelation AS b ON a.RoomId = b.RoomId LEFT OUTER JOIN
                                                      WY_Owner AS c ON b.OwnerId = c.OwnerId LEFT OUTER JOIN
                                                      WY_Area AS d ON a.AreaCode = d.AreaCode LEFT OUTER JOIN
                                                      WY_Building AS e ON a.BuildingCode = e.BuildingCode
                                WHERE     (a.UseType IN (1, 2)) AND (a.DeleteFlag = 0) AND (a.AreaCode = '{0}') AND (a.BuildingCode = '{1}') and (a.RoomNumber like '%{2}%' or c.Name like '%{2}%')
                                ORDER BY a.RoomNumber";
            page.KeyName = "RoomNumber";
            strsql = string.Format(strsql, _areacode, _buildingCode, _roomname);
            strsql = CoreFrame.Common.FilterString.FormatSql(strsql, page, oleDb);
            return oleDb.GetDataTable(strsql);
        }

        public DataTable GetAllRoomList()
        {
            string strsql = @"SELECT     a.RoomId,a.RoomNumber,b.ownerid,(select name from wy_owner where ownerid=b.ownerid) OwnerName FROM  WY_Room AS a LEFT OUTER JOIN WY_RoomOwnerRelation AS b ON a.RoomId = b.RoomId WHERE (a.UseType IN (1, 2)) AND (a.DeleteFlag = 0)";
            return oleDb.GetDataTable(strsql);
        }

        

        public DataTable GetShopList(string _shopname, CoreFrame.Core.Orm.PageInfo page)
        {
            string strsql = @"SELECT     ShopId, ShopName, UseArea, OwnerId, OwnerName, Memo
                                FROM         WY_Shop
                                WHERE     (DeleteFlag = 0) AND (ShopName LIKE '%{0}%')";
            page.KeyName = "ShopId";
            strsql = string.Format(strsql, _shopname);
            strsql = CoreFrame.Common.FilterString.FormatSql(strsql, page, oleDb);
            return oleDb.GetDataTable(strsql);
        }

        public DataTable GetAllShopList()
        {
            string strsql = @"SELECT  ShopId,ShopName, OwnerId,OwnerName  FROM WY_Shop WHERE     (DeleteFlag = 0) ";
            return oleDb.GetDataTable(strsql);
        }

        public bool DeleteOSRCCItemRelation(string SrccId)
        {
            string strsql = "delete from WY_OSRCCItemRelation where SrccId='" + SrccId + "'";
            oleDb.DoCommand(strsql);
            return true;
        }

        public bool DeleteMeter(int type,int type2,string SrccId)
        {
            string strsql = null;
            if (SrccId == null)
            {
                strsql = @"delete from WY_Meter where MeterType={0} and MeterType2={1} and SrccId is NULL";
                strsql = string.Format(strsql, type, type2);
            }
            else
            {
                strsql = @"delete from WY_Meter where MeterType={0} and MeterType2={1} and SrccId ='{2}'";
                strsql = string.Format(strsql, type, type2,SrccId);
            }

            return oleDb.DoCommand(strsql) > 0;
        }
    }
}
